package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreateParameterCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateParameterCommand;
import com.wiscamp.ninechapters.panels.application.services.ParameterAppService;
import com.wiscamp.ninechapters.panels.application.vo.ParameterVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 参数服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-参数信息")
public class ParameterController extends BaseController {

    private final ParameterAppService parameterAppService;

    public ParameterController(ParameterAppService parameterAppService) {
        this.parameterAppService = parameterAppService;
    }

    @GetMapping("Parameters/{parameterId}")
    @Operation(summary = "通过编号获取参数")
    @HandleResourceException
    public RestResponse<ParameterVO> getParameter(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                  @PathVariable("parameterId") long parameterId) {
        return ResourceUtils.getById(request, response, parameterId, parameterAppService::getParameter);
    }


    @GetMapping("Applications/{appId}/Parameters/Code/{parameterCode}")
    @Operation(summary = "通过代码获取参数")
    @HandleResourceException
    public RestResponse<ParameterVO> getParameterByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                        @PathVariable("appId") long appId, @PathVariable("parameterCode") String parameterCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, parameterCode, parameterAppService::getParameterByCode);
    }

    @GetMapping("Applications/{appId}/Parameters/Name/{parameterName}")
    @Operation(summary = "通过名称获取参数列表")
    @HandleResourceException
    public RestResponse<Collection<ParameterVO>> getParametersByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                     @PathVariable("appId") long appId, @PathVariable("parameterName") String parameterName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, parameterName, parameterAppService::getParametersByName);
    }

    @GetMapping("Applications/{appId}/Parameters/Group/{parameterGroup}")
    @Operation(summary = "通过分组获取参数列表")
    @HandleResourceException
    public RestResponse<Collection<ParameterVO>> getParametersByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                      @PathVariable("appId") long appId, @PathVariable("parameterGroup") String dictionarGroup) {
        return ResourceUtils.getListByIdAndText(request, response, appId, dictionarGroup, parameterAppService::getParametersByGroup);
    }

    @PostMapping("Parameters/Query")
    @Operation(summary = "分页获取参数列表")
    @HandleResourceException
    public RestResponse<Collection<ParameterVO>> getParametersByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                      @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, parameterAppService::getParametersByQuery);
    }

    @PostMapping("Parameters")
    @Operation(summary = "创建参数")
    @HandleResourceException
    public RestResponse<ParameterVO> createParameter(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateParameterCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, parameterAppService::createParameter);
    }


    @PutMapping("Parameters")
    @Operation(summary = "更新参数")
    @HandleResourceException
    public RestResponse<ParameterVO> updateParameter(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateParameterCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, parameterAppService::updateParameter);
    }

    @PutMapping("Parameters/Actions/Delete")
    @Operation(summary = "通过编号删除参数")
    @HandleResourceException
    public RestResponse<Boolean> deleteParameter(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, parameterAppService::deleteParameter);
    }

    @PutMapping("Parameters/Actions/Enable")
    @Operation(summary = "通过编号启用参数")
    @HandleResourceException
    public RestResponse<Boolean> enableParameter(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, parameterAppService::enableParameter);
    }

    @PutMapping("Parameters/Actions/Disable")
    @Operation(summary = "通过编号禁用参数")
    @HandleResourceException
    public RestResponse<Boolean> disableParameter(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, parameterAppService::disableParameter);
    }
}
